package cn.zhibang.yygh2.service.hosp.service.impl;


import cn.zhibang.yygh2.common.util.exception.YyghException;
import cn.zhibang.yygh2.common.util.result.ResultCodeEnum;
import cn.zhibang.yygh2.model.model.hosp.HospitalSet;
import cn.zhibang.yygh2.service.hosp.mapper.HospitalSetMapper;
import cn.zhibang.yygh2.service.hosp.service.HospitalSetService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    //    自动装配Mapper
    @Resource
    private HospitalSetMapper hospitalSetMapper;


    /**
     * 根据医院code获取签名key
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getSignKey(String hoscode) {
//调用方法，传入医院code，查询医院设置数据
        HospitalSet hospitalSet = this.getByHoscode(hoscode);
//如果医院设置数据为null，表示该医院code不存在
        if (null == hospitalSet) {
            //抛异常" 医院未开通，暂时不能访问 "
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
//如果医院状态的值为0，表示该医院账号已锁定
        if (hospitalSet.getStatus().intValue() == 1) {
            //抛异常" 医院被锁定，暂时不能访问 "
            throw new YyghException(ResultCodeEnum.HOSPITAL_LOCK);
        }
//返回医院设置中的密钥
        return hospitalSet.getSignKey();
    }

    /**
     * 根据hoscode获取医院设置
     *
     * @param hoscode
     * @return
     */
    private HospitalSet getByHoscode(String hoscode) {
        return hospitalSetMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
    }
}
